import 'package:flutter/material.dart';
import 'home_page.dart';
import 'category_page.dart';
import 'cart_page.dart';
import 'member_page.dart';

class IndexPage extends StatefulWidget {
  @override
  _IndexPageState createState() => _IndexPageState();
}

class _IndexPageState extends State<IndexPage> with SingleTickerProviderStateMixin{
  //数组存储底部导航栏按钮
  final List<BottomNavigationBarItem> bottomTabs = [
    BottomNavigationBarItem(
      icon: Icon(Icons.home),
      activeIcon: Chip(
          backgroundColor: Color.fromRGBO(68, 138, 255, 0.1),
          avatar: Icon(Icons.home),
          label: Text('首页', style: TextStyle(color: Colors.blueAccent))),
      title: Text(''),
    ),
    BottomNavigationBarItem(
      icon: Icon(Icons.search),
      activeIcon: Chip(
          backgroundColor: Color.fromRGBO(255, 171, 64, 0.1),
          avatar: Icon(
            Icons.search,
            color: Colors.orangeAccent,
          ),
          label: Text('分类', style: TextStyle(color: Colors.orangeAccent))),
      title: Text(''),
    ),
    BottomNavigationBarItem(
      icon: Icon(Icons.shopping_cart),
      activeIcon: Chip(
        backgroundColor: Color.fromRGBO(224, 64, 251, 0.1),
        avatar: Icon(
          Icons.shopping_cart,
          color: Colors.purpleAccent,
        ),
        label: Text(
          '购物车',
          style: TextStyle(color: Colors.purpleAccent),
        ),
      ),
      title: Text(''),
    ),
    BottomNavigationBarItem(
      icon: Icon(Icons.person),
      activeIcon: Chip(
          backgroundColor: Color.fromRGBO(255, 64, 129, 0.1),
          avatar: Icon(
            Icons.person,
            color: Colors.pinkAccent,
          ),
          label: Text('个人', style: TextStyle(color: Colors.pinkAccent))),
      title: Text(''),
    ),
  ];

  final List<Widget> tabBodies = [
    HomePage(),
    CategoryPage(),
    CartPage(),
    MemberPage()
  ];

  int _currentIndex = 0;

  TabController _tabController; //需要定义一个Controller
  @override
  void initState() {
    super.initState();
    // 创建Controller
    _tabController = TabController(length: tabBodies.length, vsync: this);
  }

  @override
  Widget build(BuildContext context) {
    _tabController.addListener((){
      setState(() {
        _currentIndex=_tabController.index;
      });
    });
    return Scaffold(
      bottomNavigationBar: BottomNavigationBar(
        type: BottomNavigationBarType.fixed,
        elevation: 20,
        selectedFontSize: 0,
        unselectedFontSize: 0,
        currentIndex: _currentIndex,
        items: bottomTabs,
        onTap: (index) {
          setState(() {
            _currentIndex = index;
            _tabController.index=index;
          });
        },
      ),
      body: TabBarView(
          controller:_tabController,
          children: tabBodies
      ),
//      body: IndexedStack(
//        index: _currentIndex,
//        children: tabBodies,
//      ),
    );
  }
}
